#import "BLEForUnity.h"

@interface BLEForUnity () <CBCentralManagerDelegate, CBPeripheralDelegate> {
   
}
@property(nonatomic,retain) NSMutableArray *sendArray;
@end

@implementation BLEForUnity
+ (SGBleManager*)Instance {
    static BLEForUnity* singleton;
    static dispatch_once_t token;
    dispatch_once(&token,^{
        singleton = [[BLEForUnity alloc] initSingle];
    });
    return singleton;
}
- (id)initSingle{
    self = [super init];
    if (self) {
        _sendArray =  [NSMutableArray array];
    }
    return self;
}
- (void)BlueInit
{
     if(!_centralManager)
        _centralManager = [[CBCentralManager alloc] initWithDelegate:self
                                                               queue:nil];
      [self centralManagerDidUpdateState:_centralManager];
}

//蓝牙监听函数
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    switch (central.state) {
        case CBCentralManagerStatePoweredOff:{
            NSLog(@"~~~蓝牙关闭");
            _currentBleState = SGBleStatePoweredOff;
        }
            break;
        case CBCentralManagerStatePoweredOn: {
            NSLog(@"~~~BLE状态正常");
            _currentBleState = SGBleStatePoweredOn;
           
        }
            break;
        case CBCentralManagerStateResetting:
            _currentBleState = SGBleStateResetting;
            break;
        case CBCentralManagerStateUnauthorized:
            _currentBleState = SGBleStateUnauthorized;
            break;
        case CBCentralManagerStateUnknown:
            _currentBleState = SGBleStateUnknown;
            break;
        case CBCentralManagerStateUnsupported:
            _currentBleState = SGBleStateUnsupported;
            break;
        default:
            _currentBleState = SGBleStateUnknown;
            break;
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kSGBleManagerNotificationBleStateUpdate
                                                        object:nil
                                                      userInfo:nil];
}

